// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    isDisabled:false
  },
  // 事件处理函数
  onSumit(e) {
    let password = e.detail.value.password;
    let old_password = e.detail.value.old_password;
    let password_confirmation = e.detail.value.password_confirmation;
    let that = this;
    let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/
    if(!old_password || old_password.length<=0){
      wx.showToast({
        title: "请输入旧密码",
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if(!(reg.test(password))){
      wx.showToast({
        title: "密码必须包含数字和字符且8-16位",
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if(!password_confirmation || password_confirmation.length<=0){
      wx.showToast({
        title: "请输入确认密码",
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if(password_confirmation != password){
      wx.showToast({
        title: "密码不一致",
        icon: 'none',
        duration: 2000
      })
      return false
    }
    wx.showLoading({
      title: '保存中',
      mask:true
    })
    this.setData({
      isDisabled:true
    })
    wx.request({
      url: app.globalData.baseUrl + '/api/user/changePassword',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'token':app.globalData.token
      },
      data: {
        "password_confirmation": password_confirmation,
        "password": password,
        "old_password":old_password
      },
      success: function(res) {  
        wx.hideLoading()  
        that.setData({
          isDisabled:false
        })      
        if(res.data.status){
          wx.showToast({
            title: '保存成功',
            icon: 'none',
            duration: 2000
          })
         that.onExit()
        }else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: function() {
        wx.hideLoading()
        that.setData({
          isDisabled:false
        })  
        wx.showToast({
          title: "请求失败",
          icon: 'none',
          duration: 2000
        })
      }
    })

  },
  onExit(){
    wx.clearStorage({
      success(res){
          wx.redirectTo({
        url: '/pages/login/index',
      })
      }
    }); 
  },
  onLoad() {
   
  },

  
})
